/**
 * 
 */
package com.armail.client.view.javafx;

import java.util.Collection;
import java.util.LinkedHashSet;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

import com.armail.client.dto.Mail;
import com.armail.client.view.ComposeMailView;
import com.armail.client.view.ComposeMailViewListener;
import com.armail.client.view.ViewLocation;
import com.armail.client.view.ViewManager;
import com.google.inject.Inject;

/**
 * @author arpit
 *
 */
public class FXComposeMailView extends VBox implements ComposeMailView{
	protected final Collection<ComposeMailViewListener> listeners = new LinkedHashSet<ComposeMailViewListener>();
	private final ViewManager viewManager;
	
	private FXMailDetailsPane detailsPane;
	private final Button sendButton = new Button("Send");
	private final Button saveButton = new Button("Save");
	
	@Inject
	public FXComposeMailView(ViewManager viewManager) {
		this.viewManager = viewManager;
		setSpacing(15);
		setPadding(new Insets(15));
		getChildren().addAll(getDetailsPane(), getBottomHBox());
		VBox.setVgrow(getDetailsPane(), Priority.ALWAYS);
	}
	
	@Override
	public void attachChild(Object subView, ViewLocation location) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void detachChild(Object subView) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void attach() {
		viewManager.attach(this, ViewLocation.COMPOSE_MAIL);
	}

	private HBox getBottomHBox() {
		
		sendButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				for (ComposeMailViewListener l : listeners) {
					l.onMailSend();
				}
			}
		});
		
		saveButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				for (ComposeMailViewListener l : listeners) {
					l.onMailSave();
				}
			}
		});

		HBox bottonHBox = new HBox();
		bottonHBox.setSpacing(20);
		bottonHBox.setAlignment(Pos.CENTER_RIGHT);
		bottonHBox.getChildren().addAll(saveButton, sendButton);
		return bottonHBox;
	}

	@Override
	public void detach() {
		viewManager.unregisterLocations(this);
		viewManager.detach(this, ViewLocation.COMPOSE_MAIL);
	}

	@Override
	public void done() {
		setDisable(true);}

	@Override
	public void addListener(ComposeMailViewListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public void removeListener(ComposeMailViewListener listener) {
		this.listeners.remove(listener);
	}

	@Override
	public void setModel(Mail model) {
		getDetailsPane().setModel(model);
	}
	
	public FXMailDetailsPane getDetailsPane() {
		if(detailsPane == null) {
			detailsPane = new FXMailDetailsPane();
		}
		return detailsPane;
	}
}
